﻿



create proc Products.SaveProductAttributes (
	@ProductID		uniqueidentifier,
	@AttributeID	uniqueidentifier,
	@ErrorMessage	nvarchar(400) output
)
as
begin

	if @ProductID is null
	begin
		set @ErrorMessage = 'ProductID ist null';
		goto EndWithError;
	end
	
	if @AttributeID is null
	begin
		set @ErrorMessage = 'AttributeID ist null';
		goto EndWithError;
	end
	
	begin tran;
	
	with src 
	as
	(
		Select	@ProductID 'ProductID',
				@AttributeID 'AttributeID'
	)
	
	merge Products.datProductAttributes as target
	using src
		on (target.ProductID = src.ProductID and target.AttributeID = src.AttributeID)
	when not matched by target then
		insert (
			ProductID,
			AttributeID
		)
		values (
			src.ProductID,
			src.AttributeID
		);
		
	
	commit;
	return 0;
	
EndWithError:
	RAISERROR ( @ErrorMessage, 16, 1);
	return 1;	

end
GO


